package com.yuexiao.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuexiao.entity.ResultInfo;
import com.yuexiao.pojo.Setmeal;
import com.yuexiao.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName:SetmealController
 * @Auther: YUEXIAO
 * @Description: 套餐操作控制器
 * @Date: 2022/8/12 15:57
 * @Version: v1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Reference
    private SetmealService setmealService;


    /**
     * 根据条件分页查询套餐数据
     * @param page int 当前页
     * @param pageSize int 每页显示条数
     * @param name String 套餐名
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        return ResultInfo.success(setmealService.getSetmealPage(page,pageSize,name));
    }


    /**
     * 新增套餐
     * @param setmeal Setmeal 套餐实体类
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/addSetmeal")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo addSetmeal(@RequestBody Setmeal setmeal){
        log.info(setmeal.toString());
        return ResultInfo.success(setmealService.insertSetmeal(setmeal));
    }


    /**
     * 根据套餐id查询套餐数据
     * @param id Long 套餐id
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/querySetmealById/{id}")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo querySetmealById(@PathVariable Long id){
        return ResultInfo.success(setmealService.getSetmealById(id));
    }


    /**
     * 更新套餐数据
     * @param setmeal Setmeal 套餐对象
     * @return ResultInfo 返回结果对象
     */
    @PutMapping("/editSetmeal")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo editSetmeal(@RequestBody Setmeal setmeal){
        return ResultInfo.success(setmealService.updateSetmeal(setmeal));
    }


    /**
     * 修改套餐上架状态
     * @param status int 更改状态
     * @param ids Long[] 主键数组
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/setmealStatusByStatus/{status}")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo setmealStatusByStatus(@PathVariable int status,Long[] ids){
        return ResultInfo.success(setmealService.updateStatusByIds(status, ids));
    }


    /**
     * 删除套餐数据
     * @param ids Long[] id数组
     * @return ResultInfo 返回结果对象
     */
    @DeleteMapping("/deleteSetmeal")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo deleteSetmeal(Long[] ids){
        return ResultInfo.success(setmealService.deleteSetmeal(ids));
    }

}
